#include <iostream>

using namespace std;
 int com( int y,int m,int d);                     //计算天数
 string week(int y,int m,int d);                   //计算星期
 int date(int y,int m,int d);                     //由天数计算日期
 bool dis(int ye,int mo,int da);             //日期是否正确
 int leap(int ye);                                           //判断是平年还是闰年
 int dateday();                                               //查询天数
 int difference();                                           //计算相差多少天
 int daydate();                                              //计算日期

int main()
{
    int a;//选择的功能代号
    cout<<"Please select the features you need"<<endl;
    cout<<"Input1,query a date is the year of the first few days, days of the week"<<endl;
    cout<<"Input 2,query how many days difference between two dates"<<endl;
    cout<<"Input 3,enter the year and the number of days, check the date and the day of the week"<<endl;
    cin>>a;
    switch(a)
    {
        case 1:dateday();break;
        case 2:difference();break;
        case 3:daydate();break;
        default:cout<<"error"<<endl;break;
    }
    return 0;
}
int dateday()           //输入日期查询天数 星期
{
    int year;
    int moon;
    int day;
    cout << "Please input one date" << endl;
    cin>>year>>moon>>day;
    if(1==leap(year))                                                                                               //闰年
        {
            if(dis(year,moon,day))                                                                                  //判断日期是否正确
            {
                    cout <<year<< "year is leap year"<<moon<<"moon "<<day<<"day is " <<com(year,moon,day)<<" day is "<<week(year,moon,day)<< endl;
                    }//if判断日期是否正确
            else
            {
                        cout << "error"<< endl;
                        }
        }//闰年
    else             //平年
        {
            if(dis(year,moon,day))        //日期正确
            {
                    cout <<year<< "year is nonleap year "<<moon<<"moon "<<day<<"day is " <<com(year,moon,day)<<" day is "<<week(year,moon,day)<< endl;
                    }
            else
            {
                        cout << "error"<< endl;
                    }
        } //平年
    return 0;
}//查询天数 星期
int difference()            //计算一年中两个日期相差多少天
{
    int years,moon1,day1,moon2,day2;
    cout << "Please input two date" << endl;
    cin>>years>>moon1>>day1>>moon2>>day2;
    if(dis(years,moon1,day1)&&dis(years,moon2,day2))
    {
        if(com(years,moon1,day1)>com(years,moon2,day2))
        {
            cout << "The difference is " <<com(years,moon1,day1)-com(years,moon2,day2)<<" day"<< endl;
            }
        else
        {
            cout << "The difference is " <<com(years,moon2,day2)-com(years,moon1,day1)<<" day"<< endl;
            }
        }
    else
    {
        cout << "error"<< endl;
    }
    return 0;
}  //计算一年中两个日期相差多少天
int daydate()               //输入天数计算日期
{
    int year;//输入的年份
    int moon=0;//初始化日期的月份
    int day;//输入的天数
    cout<<"Please enter the year and the number of days"<<endl;
    cin>>year>>day;
    if(((1==leap(year)&&day<=366)||(0==leap(year)&&day<=365))&&year!=0)
    {
        date(year,moon,day);
        }
    else
    {
        cout << "error"<< endl;
        }
    return 0;
}
int com( int y, int m,int d)
{
            if (1==m)
            {return d;}
            d+=31;
            if(2==m)
            {return d;}
            if(1==leap(y)){d+=29;}                                                                                               //闰年
            else{d+=28;}
            if(3==m)
            {return d;}
            d+=31;
            if(4==m)
            {return d;}
            d+=30;
            if(5==m)
            {return d;}
            d+=31;
            if(6==m)
            {return d;}
            d+=30;
            if(7==m)
            {return d;}
            d+=31;
            if(8==m)
            {return d;}
            d+=31;
            if(9==m)
            {return d;}
            d+=30;
            if(10==m)
            {return d;}
            d+=31;
            if(11==m)
            {return d;}
            d+=30;
            if(12==m)
            {return d;}
}
 bool dis(int ye,int mo,int da)
 {
        bool temp=false;
        if(mo<=12&&ye!=0){
                switch(mo){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if(da<=31)
                    temp = true;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    if(da<=30)
                    temp=true;
                    break;
                case 2:
                    if(1==leap(ye)&&da<=29)
                        temp = true;
                    if(0==leap(ye)&&da<=28)
                        temp = true;
                    break;
                default:
                    temp = false;
                    break;
                }
        }
        return temp;
}
int leap(int ye)
{
    int lea;
         if((ye%4==0&&ye%100!=0)||ye%400==0)
             {
                lea=1;
                }
         else lea=0;
     return lea;
}
int date(int y,int m,int d)
{
    m=1;
    if (d<=31){}//1月
    else{
            d-=31;m+=1;
            if(1==leap(y))//闰年
                {
                    if(d<=29){}//2
                    else{
                            d-=29;m+=1;
                            if(d<=31){}//3
                            else{
                                    d-=31;m+=1;
                                    if(d<=30){}//4
                                    else{
                                            d-=30;m+=1;
                                            if(d<=31){}//5
                                            else{
                                                    d-=31;m+=1;
                                                    if(d<=30){}//6
                                                    else{
                                                            d-=30;m+=1;
                                                            if(d<=31){}//7
                                                            else{
                                                                    d-=31;m+=1;
                                                                    if(d<=31){}//8
                                                                    else{
                                                                            d-=31;m+=1;
                                                                            if(d<=30){}//9
                                                                            else{
                                                                                    d-=30;m+=1;
                                                                                    if(d<=31){}//10
                                                                                    else{
                                                                                            d-=31;m+=1;
                                                                                            if(d<=30){}//11
                                                                                            else{
                                                                                                    d-=30;m+=1;//12
                                                                                                        }//12
                                                                                                }//11
                                                                                        }//10
                                                                                }//9
                                                                        }//8
                                                                }//7
                                                        }//6
                                                }//5
                                        }//4
                                }//3
                        }//2
            else  if(0==leap(y))//平年
                {
                    if(d<=28){}//2
                    else{
                            d-=28;m+=1;
                            if(d<=31){}//3
                            else{
                                    d-=31;m+=1;
                                    if(d<=30){}//4
                                    else{
                                            d-=30;m+=1;
                                            if(d<=31){}//5
                                            else{
                                                    d-=31;m+=1;
                                                    if(d<=30){}//6
                                                    else{
                                                            d-=30;m+=1;
                                                            if(d<=31){}//7
                                                            else{
                                                                    d-=31;m+=1;
                                                                    if(d<=31){}//8
                                                                    else{
                                                                            d-=31;m+=1;
                                                                            if(d<=30){}//9
                                                                            else{
                                                                                    d-=30;m+=1;
                                                                                    if(d<=31){}//10
                                                                                    else{
                                                                                            d-=31;m+=1;
                                                                                            if(d<=30){}//11
                                                                                            else{
                                                                                                    d-=30;m+=1;//12
                                                                                                        }//12
                                                                                                }//11
                                                                                        }//10
                                                                                }//9
                                                                        }//8
                                                                }//7
                                                        }//6
                                                }//5
                                        }//4
                                }//3
                        }//2
                }//1
        cout<<"The date is "<<y<<"year "<<m<<"moon "<<d<<"day "<<week(y,m,d)<<endl;
}
  string week(int y,int m,int d)
  {
        int x;
        int i=0;
        int w;
        string week;
        if(y>=1)
            {
                for(x=1;x<y;x++)
                i+=365+leap(x);
                }
        else if(y<=(-1))
            {
                for(x=1;x<(-y);x++)
                i-=365-leap(x);
                }
        w=(i+com(y,m,d))%7;
        switch(w)
        {
            case 0:week="Sunday";break;
            case 1:week="Monday";break;
            case 2:week="Tuesday";break;
            case 3:week="Wednesday";break;
            case 4:week="Thursday";break;
            case 5:week="Friday";break;
            case 6:week="Saturday";break;
            default:;break;
            }
        return week;
  }
